package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.Evaluation;
import com.ruoyi.business.domain.bo.EvaluationBo;
import com.ruoyi.business.domain.vo.EvaluationVo;
import com.ruoyi.business.domain.vo.HomeworkStuAnswerVo;
import com.ruoyi.business.domain.vo.QuestionVo;
import com.ruoyi.business.mapper.EvaluationMapper;
import com.ruoyi.business.mapper.HomeworkStuAnswerMapper;
import com.ruoyi.business.service.IEvaluationService;
import com.ruoyi.business.service.IQuestionService;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author 最爱喝兽奶
 * @create 2022/9/29
 **/

@SuppressWarnings("ALL")
@Service
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation> implements IEvaluationService {

    @Autowired
    private HomeworkStuAnswerMapper homeworkStuAnswerMapper;

    @Autowired
    private IQuestionService questionService;

    @Override
    public void submitEvaluation(String homeworkId, Integer copies, Date endTime) {
        // 查询出所有可以参与互评的学生信息
        List<String> list = homeworkStuAnswerMapper.queryEvaluationStudent(homeworkId);
        List<Evaluation> evaluations = new ArrayList<>();
        // 这一层的 for 表示人,每个人需要审核 5 个人的作业
        Integer group = list.size() / copies + 1;
        for (int i = 0; i < list.size(); i++) {
            // 这一层的 for 表示被审核的作业的人的id
            Integer step = 0;
            for (int j = i; step < copies; j = (j + group) % list.size()) {
                if (i != j) {
                    Evaluation evaluation = new Evaluation();
                    evaluation.setHomeworkId(homeworkId);
                    evaluation.setEvaluationUserId(list.get(i));
                    evaluation.setResponseUserId(list.get(j));
                    evaluation.setEndTime(endTime);
                    evaluations.add(evaluation);
                    step++;
                }
            }
        }
        saveBatch(evaluations);
    }

    @Override
    public TableDataInfo<EvaluationVo> selectByPage(EvaluationBo bo) {
        Page<EvaluationVo> page = baseMapper.selectByPage(PageUtils.buildPage(bo), bo);
        List<EvaluationVo> collect = page.getRecords().stream()
                .filter(new Predicate<EvaluationVo>() {
            @Override
            public boolean test(EvaluationVo evaluationVo) {
                try {
                    // 判断当前查询的有没有过截止日期
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    Date endTime = sdf.parse(evaluationVo.getEndTime());
                    if (endTime.getTime() > new Date().getTime()) {
                        return true;
                    }else {
                        return false;
                    }
                } catch (Exception e) {
                    return false;
                }
            }
        }).collect(Collectors.toList());
        return PageUtils.buildTableDataInfo(page);
    }

    @Override
    public List<QuestionVo> queryQuestionsByEvaluation(EvaluationBo bo) {
        // 查询出问题的id
        List<HomeworkStuAnswerVo> homeworkStuAnswerVos = baseMapper.queryQuestionByEvaluation(bo);
        List<String> collect = homeworkStuAnswerVos.stream().map(item -> {
            return item.getQuestionId();
        }).collect(Collectors.toList());
        // 根据传入的问题id列表，将问题详情查询出来
        List<QuestionVo> list = questionService.selectQuestionsByIds(collect);
        // 将list的用户作答区设置答案
        for (QuestionVo vo : list) {
            for (HomeworkStuAnswerVo homeworkStuAnswerVo : homeworkStuAnswerVos) {
                if (vo.getId().equals(homeworkStuAnswerVo.getQuestionId())) {
                    vo.setStuSubAnswer(homeworkStuAnswerVo.getAnswer());
                }
            }
        }
        return list;
    }

}
